﻿package com.PlantVsZombies.Plants;

import java.awt.Graphics;
import java.util.ArrayList;

import com.PlantVsZombies.Bullet.Bullet;
import com.PlantVsZombies.common.Plant;
import com.PlantVsZombies.constant.PlantType;
import com.PlantVsZombies.util.ImageUtil;
import com.PlantVsZombies.util.MusicPlay;

/**
 * 
 * 豌豆射手类
 * 
 * @author Administrator
 *
 */
public class PeaPlant extends Plant implements Runnable {

	int imageIndex = 0; // 图片标记
	int bulletspeed = 20; // 发射子弹速度
	int shootIndex = 0; // 发射子弹标记
	MusicPlay mp = new MusicPlay("music/throw.wav");

	// 子弹集合
	ArrayList<Bullet> bullet = new ArrayList<Bullet>();

	// 获取子弹集合
	public ArrayList<Bullet> getBullet() {
		return bullet;
	}

	public void setBullet(ArrayList<Bullet> bullet) {
		this.bullet = bullet;
	}

	public PeaPlant(int x, int y) {
		this.x = x;
		this.y = y;
		this.plantImage = ImageUtil.PEAPLANT;
		this.canShoot = false;
		this.life = 50;
	}

	@Override
	public void drawMe(Graphics g, int x, int y) {
		this.x = x;
		this.y = y;
		drawBullet(g);
		g.drawImage(plantImage.getImage(), x, y, null);
	}

	public void drawBullet(Graphics g) {
		for (int i = 0; i < bullet.size(); i++) {
			Bullet bt = bullet.get(i);
			if (bt.isLive()) {
				bt.drawMe(g);
			} else
				bullet.remove(bt);
		}
	}

	@Override
	public void shootBullet() {
		Bullet bt = new Bullet(x, y);
		bt.start();
		bullet.add(bt);
		mp.replay();
	}

	@Override
	public PlantType getType() {
		return PlantType.SingleBullet;
	}

	@Override
	public int doRun() {
		if (isAttacked && life > 0) {
			life--;
		}
		if (life == 0) {
			this.setLive(false);
			this.isAttacked = false;
			return 0;
		}
		try {
			Thread.sleep(150);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// 可以发射则发射
		if ((shootIndex++ % bulletspeed == 0) && (this.canShoot == true)) {
			shootBullet();
		}
		return 1;
	}

}
